package com.zjitc.controller;

import com.google.gson.Gson;
import com.zjitc.model.intelligent.Chart;
import com.zjitc.model.intelligent.ChartContent;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Create by IntelliJ IDEA
 *
 * @author: lhl
 * @date-Time: 2018/6/5 0005
 * @description:
 */
@Controller
public class LaborControl {

    @RequestMapping(value = "/intelligentReplay", produces = "text/html; charset=UTF-8")
    @ResponseBody
    public String replay(String msg, HttpSession session) {
        Integer count = (Integer) session.getAttribute("intelligentReplay");
        if (count == null) {
            int i = 1;
            session.setAttribute("intelligentReplay", i);
        } else {
            session.setAttribute("intelligentReplay", count + 1);
        }
        ChartContent chart = new ChartContent();
        String answer = null;
        try {
            answer = chart.connection(msg);

        } catch (IOException e) {
            e.printStackTrace();
        }
        Gson gson = new Gson();
        Chart answerChart = gson.fromJson(answer, Chart.class);
        int temCount = 2;
        if (count != null && count > temCount) {
            return
                    " 小Q理解不了您的问题，请点击下方咨询人工客服<a target=\"_blank\" href=\"http://wpa.qq.com/msgrd?v=3&uin=1694190210&site=qq&menu=yes\">\n"
                            + "http://wpa.qq.com/msgrd" +
                            " </a>" + "或者点击左上角QQ洽谈";
        }
        int temCode = 100000;
        if (answerChart.getCode() == temCode && answerChart.getText() != null) {
            return answerChart.getText();
        }
        return "请转人工客服";
    }
}
